package com.qf.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.entity.NewsComment;
import com.qf.mapper.NewsCommentMapper;
import com.qf.service.NewsCommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author lenovo
 * @version V1.0
 * @Date 2021/10/6 0:41
 */
@Service
public class NewsCommentServiceImpl implements NewsCommentService {
    @Autowired
    private NewsCommentMapper newsCommentMapper;

    @Override
    public List<NewsComment> findAllByNewsId(Integer id) {
        return newsCommentMapper.findAllByNewsId(id);
    }

    @Override
    public PageInfo<NewsComment> findAllByNewsIdAndPage(Integer num, Integer size, Integer id) {
        PageHelper.startPage(num,size);

        List<NewsComment> commentList = newsCommentMapper.findAllByNewsId(id);

        PageInfo<NewsComment> pageInfo = new PageInfo<>(commentList);

        return pageInfo;
    }

    @Override
    @Transactional(rollbackFor = Throwable.class)
    public void add(NewsComment newsComment) {
        int i = newsCommentMapper.insert(newsComment);
        if (i != 1){
            System.out.println("添加失败");
            throw new RuntimeException("添加失败");
        }

    }
    @Override
    @Transactional(rollbackFor = Throwable.class)
    public void delete(Integer id) {

        int i = newsCommentMapper.deleteByPrimaryKey(id);
        if (i != 1){
            System.out.println("删除失败");
            throw new RuntimeException("删除失败");
        }

    }
}
